local skel = fk.CreateSkill {
  name = "lb_zero__xiali",
  max_branches_use_time = function(self, player)
    local room = Fk:currentRoom()
    local list = {}
    for _, to in ipairs(room.players) do
      list[tostring(to.id)] = { [Player.HistoryGame] = 3 }
    end
    return list
  end,
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable {
  ["lb_zero__xiali"] = "黠狸",
  [":lb_zero__xiali"] = "每名角色限三次，有角色摸牌时，你可令其改为获得等量参与伤害结算后可重铸一张的【影】，且你可耗尽对其的〖黠狸〗次数，以令其额外获得等量倍的【影】。",

  ["@[skill_players]lb_zero__xiali"] = "黠狸",
  ["@@lb_zero__xiali-inarea-can_recast"] = "黠狸",

  ["#lb_zero__xiali"] = "黠狸：%src 即将摸 %arg 张牌，你可以令其改为获得 %arg 张【影】",
  ["#lb_zero__xiali-draw"] = "黠狸：%src 即将摸 %arg 张牌，你可以令其改为获得 %arg 张【影】，或耗尽对其的〖黠狸〗次数，以令其改为获得 %arg2 张【影】",

  ["$lb_zero__xiali1"] = "恶作剧时间~",
  ["$lb_zero__xiali2"] = "可别被吓到哦~",
  ["$lb_zero__xiali3"] = "来嘛来嘛~别紧张啊~~",
}

skel:addEffect(fk.BeforeDrawCard, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and data.num > 0 and
        skel:withinBranchTimesLimit(player, tostring(target.id), Player.HistoryGame)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = 1
    local use_num = 3 - player:usedSkillTimes(skel.name, Player.HistoryGame, tostring(target.id))
    local choices = {tostring(data.num)}
    if use_num > 1 then
      table.insert(choices, tostring(data.num * use_num))
    end
    local choice = choices[1]
    if #choices > 1 then
      table.insert(choices, "Cancel")
      choice = room:askToChoice(player, {
        choices = choices,
        skill_name = skel.name,
        prompt = "#lb_zero__xiali-draw:" .. target.id .. "::" .. data.num .. ":" .. data.num * use_num,
      })
      if choice == "Cancel" then return end
      if table.indexOf(choices, choice) == 2 then
        num = use_num
      end
    else
      if not room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb_zero__xiali:" .. target.id .. "::" .. data.num }) then
        return
      end
    end
    event:setCostData(self, { num = num, tos = { target} })
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local use_num = event:getCostData(self).num
    player:addSkillBranchUseHistory(skel.name, tostring(target.id), use_num)
    local num = use_num * data.num
    data.num = 0
    room:moveCards({
      ids = U.getShade(room, num),
      to = target,
      toArea = Card.PlayerHand,
      moveReason = fk.ReasonJustMove,
      proposer = target,
      skillName = skel.name,
      moveVisible = true,
      moveMark = {"@@lb_zero__xiali-inarea-can_recast", {Card.PlayerHand, Card.PlayerEquip, Card.PlayerJudge, Card.PlayerSpecial, Card.Processing}}
    })
  end,
})



skel:addAcquireEffect(function(self, player, is_start)
  player.room:addSkill("#CanRecastCard")
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__xiali",
    { name = skel.name, scope = Player.HistoryGame, players = table.map(player.room.players, Util.IdMapper), visible = true })
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[skill_players]lb_zero__xiali", 0)
end)

return skel
